package com.example.community.list;

import com.example.community.dao.RoomDao;
import com.example.community.dao.UnitDao;
import com.example.community.model.Room;
import com.example.community.model.Unit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/roomListServlet")
public class RoomListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer ys = null;
        if (req.getParameter("ys")==null){
            ys = 1 ; //当前页数
        }else {
            ys = Integer.parseInt(req.getParameter("ys"));
        }
        int shuju = 8; //每页显示数据个数
        RoomDao roomDao = new RoomDao();
        List<Room> rooms = roomDao.fandLimit(ys, shuju);
        for (Room room:rooms){
            UnitDao unitDao = new UnitDao();
            Unit unit = unitDao.fandById(room.getUnNo());
            room.setUnName(unit.getUnName());
        }
        req.setAttribute("rooms",rooms);
        Integer totalPageNum = roomDao.selectLimitCount(shuju);
        req.setAttribute("totalPageNum",totalPageNum);
        req.setAttribute("ys",ys);
        req.getRequestDispatcher("/WEB-INF/houseManager/roomList.jsp").forward(req,resp);
    }
}
